package com.itheima.controller;

import com.itheima.pojo.*;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;
    
    /**
     * 分页查询学员列表
     */
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("分页查询学员列表，请求参数：{}", studentQueryParam);
        PageResult pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }
    /**
     * 添加学员
     */

    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学员，请求参数：{}", student);
        // 设置创建时间和更新时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentService.save(student);
        return Result.success();
    }
    /**
     * 批量删除学员
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        log.info("批量删除学员，ids: {}", ids);
        // 将逗号分隔的字符串转换为Integer列表
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());

        studentService.deleteByIds(idList);
        return Result.success();
    }
    /**
     * 根据ID查询学员
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询学员，id: {}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }
    /**
     * 修改学员信息
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息，请求参数：{}", student);
        // 设置更新时间
        student.setUpdateTime(LocalDateTime.now());
        studentService.update(student);
        return Result.success();
    }
    /**
     * 违纪处理
     */
    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理，学员ID：{}，扣除分数：{}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }



}
